package com.atguigu.flink.chapter11.function;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/10/23 9:09
 */
public class Flink04_Function_TableAgg {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<WaterSensor> stream =
            env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                             new WaterSensor("sensor_1", 2000L, 20),
                             new WaterSensor("sensor_2", 3000L, 30),
                             new WaterSensor("sensor_1", 4000L, 40),
                             new WaterSensor("sensor_1", 5000L, 50),
                             new WaterSensor("sensor_2", 6000L, 60));
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        Table table = tenv.fromDataStream(stream);
        tenv.createTemporaryView("sensor", table);
        
        // 1. 在table api中使用
        // 1.1 内联的方式
        /*table
            .groupBy($("id"))
            .flatAggregate(call(Top2Function.class, $("vc")))
            .select($("id"), $("level"), $("value"))
            .execute()
            .print();*/
    
        // 1.2 注册后使用
        tenv.createTemporaryFunction("top2", Top2Function.class);
        table
            .groupBy($("id"))
            .flatAggregate(call("top2", $("vc")))
            .select($("id"), $("level"), $("value"))
            .execute()
            .print();
    
        // 2. 在sql语句中使用
        // 不支持
        
    }
    
    public static class FirstSecond {
        public Integer first = 0;
        public Integer second = 0;
        
    }
    
    public static class Result {
        public Result(String level, Integer value) {
            this.level = level;
            this.value = value;
        }
    
        public String level;
        public Integer value;
    }
    
    public static class Top2Function extends TableAggregateFunction<Result, FirstSecond> {
        
        // 初始化累加器
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 聚合
        public void accumulate(FirstSecond fs, Integer vc) {
            if (vc > fs.first) {
                fs.second = fs.first;
                fs.first = vc;
            }else if(vc > fs.second){
                fs.second = vc;
            }
        }
        
        // 制表: 通过out.collect() 发射每行数据
        public void emitValue(FirstSecond fs, Collector<Result> out) {
            out.collect(new Result("第一名", fs.first));
            if (fs.second > 0) {
                out.collect(new Result("第二名", fs.second));
            }
        }
    }
    
}

/*
10 ..
            第一   10
            
20
            第一   20
            第二  10
30
             第一   30
             第二  20


 */